<template>
    <section>
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.name" clearable placeholder="姓名"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="filters.phone" clearable placeholder="电话"></el-input>
                </el-form-item>
                <el-form-item >
                    <el-button type="primary" @click="searchClick" >查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="default" v-on:click="resetClick">重置</el-button>
                </el-form-item>
                <el-form-item style="float:right;" >
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <el-table :data="users" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column prop="name" label="姓名" width="176"  >
            </el-table-column>
            <el-table-column prop="phone" label="手机号" width="120" >
            </el-table-column>
            <el-table-column prop="charge" label="最近充值金额" min-width="140"  >
            </el-table-column>
            <el-table-column prop="balance" label="当前剩余余额" min-width="140"  >
            </el-table-column>
            <el-table-column prop="gender" label="性别" width="90" :formatter="formatSex" >
            </el-table-column>
            <el-table-column prop="createTimeStr" label="注册时间" width="170" >
            </el-table-column>
            <el-table-column prop="status" label="状态" min-width="140"  :formatter="formatStatus" >
            </el-table-column>
            <el-table-column prop="assistant" label="创建员工" min-width="140"  :formatter="formatStatus" >
            </el-table-column>
            <el-table-column label="操作" width="280" fixed="right" >
                <template slot-scope="scope">
<!--                    <el-button type="warning" v-if="scope.row.status==1" size="small" @click="handleState(scope.$index, scope.row)">禁用</el-button>-->
<!--                    <el-button type="success" v-if="scope.row.status==0" size="small" @click="handleState(scope.$index, scope.row)">启用</el-button>-->
                    <el-button type="primary" size="small" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-pagination  layout="total,prev, pager, next,jumper" @current-change="handleCurrentChange"
                        :page-size="page.size"
                        :total="page.total" style="float:right;">
        </el-pagination>

        <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="手机号" prop="phone" >
                    <el-input type="tel" v-model="addForm.phone"  auto-complete="off" ></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="gender" >
                    <el-radio-group v-model="addForm.gender">
                        <el-radio class="radio" :label="1" >男</el-radio>
                        <el-radio class="radio" :label="2">女</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="充值金额" prop="charge" >
                    <el-input type="number" min="100" v-model="addForm.charge"  auto-complete="off" ></el-input>
                </el-form-item>
                <el-form-item label="赠送金额" prop="extra" >
                    <el-input type="number" min="100" v-model="addForm.extra"  auto-complete="off" ></el-input>
                </el-form-item>
            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>

        </el-dialog>

        <el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
            <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="editForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="手机号" prop="phone" >
                    <el-input type="tel" v-model="editForm.phone"  auto-complete="off" ></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="gender" >
                    <el-radio-group v-model="editForm.gender">
                        <el-radio class="radio" :label="1" >男</el-radio>
                        <el-radio class="radio" :label="2">女</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>

    </section>
</template>

<script>
    import {getUserListPage, removeUser, saveAdmin, saveUser} from "../../api/api";
    import {validatePhone} from "../../../static/js/validate";
    let validPhone=(rule, value,callback)=>{
        if (!value) {
            callback(new Error('请输入电话号码'))
        }else  if (!validatePhone(value)){
            callback(new Error('请输入正确的11位手机号码'))
        }else {
            callback()
        }
    }
    export default {
        name: "VipManage",
        data(){
            return {
                filters: {
                    type:3,
                    name: '',
                    phone:undefined
                },
                page:{
                    current:1,
                    total:0,
                    size:10
                },
                listLoading:false,
                users: [],
                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    name: [
                        { required: true, message: '请输入姓名', trigger: 'blur' }
                    ],
                    phone: [
                        { required: true, trigger: 'blur', validator: validPhone  }
                    ]
                },
                //编辑界面数据
                editForm: {
                    id: 0,
                    name: null,
                    gender: -1,
                    phone: null
                    // password: '',
                    // rePassword: ''
                },

                addFormVisible: false,//新增界面是否显示
                addLoading: false,
                addFormRules: {
                    name: [
                        { required: true, message: '请输入姓名', trigger: 'blur' }
                    ],
                    phone: [
                        { required: true, trigger: 'blur', validator: validPhone  }
                    ],
                    charge: [
                        { required: true, trigger: 'blur', message:'请输入重置金额'  }
                    ]
                },
                addForm:{
                    type:3,
                    name: undefined,
                    avatar: undefined,
                    gender: 1,
                    phone: undefined,
                    rePassword: undefined,
                    charge:0,
                    extra:0
                },
                sels:[]
            }
        },
        methods:{

            //新增
            addSubmit: function () {
                let _this = this;
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.addLoading = true;
                        let para = Object.assign({}, this.addForm);
                        saveUser(para).then((res) => {
                            if(res.code == 200){
                                this.addLoading = false;
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['addForm'].resetFields();
                                this.addFormVisible = false;
                                this.getUsers();
                            }else{
                                _this.$message({
                                    type: 'error',
                                    message: res.msg
                                })
                            }
                        });
                    }
                });
            },

            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.editLoading = true;
                        let para = Object.assign({}, this.editForm);
                        saveUser(para).then((res) => {
                            if(res.code == 200){
                                this.editLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: '修改成功',
                                    type: 'success'
                                });
                                this.$refs['editForm'].resetFields();
                                this.editFormVisible = false;
                                this.getUsers();
                            }
                        });
                    }
                });
            },

            handleCurrentChange(val){
                this.page.current = val
                this.getUsers()
            },
            searchClick(){
                this.page.current = 1
                this.getUsers()
            },
            resetClick(){
                this.filters.name = null
                this.filters.phone = null
                this.page.current = 1
                this.getUsers()
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //性别显示转换
            formatSex: function (row, column) {
                return row.gender == 1 ? '男' : row.gender == 2 ? '女' : '未知';
            },
            formatStatus: function(row,column){
                return   row.status == 1 ? '正常' : row.status == 0 ? '已禁用' : '未知';
            },
            handleState: function(index,row){
                let question = '确定要禁用该用户吗？'
                let user  ={
                    id: row.id,
                    status: 0
                }
                if(row.status==0){
                    question = '确定要启用该用户吗？'
                    user.status = 1;
                }
                this.$confirm(question, '提示', {}).then(() => {
                    this.editLoading = true;
                    saveUser(user).then((res) => {
                        if(res.code == 200){
                            this.editLoading = false;
                            //NProgress.done();
                            this.$message({
                                message: user.status==0?'已禁用':'已启用',
                                type: 'success'
                            });
                            // this.$refs['editForm'].resetFields();
                            // this.editFormVisible = false;
                            this.getUsers();
                        }
                    });
                })
            },
            handleEdit: function (index, row) {
                this.editLoading = false;
                this.editFormVisible = true;
                let user = Object.assign({}, row);
                this.editForm.id = user.id
                this.editForm.name = user.name
                this.editForm.phone = user.phone
                this.editForm.gender = user.gender
                console.log(this.editForm)
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = { id: row.id };
                    removeUser(para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getUsers();
                    });
                }).catch(() => {

                });
            },
            handleAdd(){
                this.editLoading = false;
                this.addFormVisible = true;
                this.addForm = {
                    name: '',
                    username: '',
                    avatar: '',
                    gender: 1,
                    phone: '',
                    password: '',
                    rePassword: '',
                    role: []
                };
            },
            getUsers(){
                let para = {
                    type: "3",
                    page: this.page.current,
                    rowSize:this.page.size,
                    name: this.filters.name,
                    phone:this.filters.phone
                };
                this.listLoading = true;
                getUserListPage(para).then((res) => {
                    let data = res.data.data;
                    console.log(data,'data')
                    this.page.total = data.total;
                    this.users = data.records;
                    this.listLoading = false;
                });
            }
        },
        created() {
            this.getUsers()
        }
    }
</script>

<style scoped>

</style>